package com.tarena.lbs.basic.web.service;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.web.repository.UserGroupRepository;
import com.tarena.lbs.basic.web.repository.UserRepository;
import com.tarena.lbs.basic.web.repository.UserTagsRepository;
import com.tarena.lbs.common.basic.util.RandomUserName;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.basic.bo.AdminBO;
import com.tarena.lbs.pojo.basic.bo.UserBO;
import com.tarena.lbs.pojo.basic.bo.UserGroupBO;
import com.tarena.lbs.pojo.basic.dos.UserTagsDO;
import com.tarena.lbs.pojo.basic.param.UserTagsParam;
import com.tarena.lbs.pojo.basic.query.UserGroupQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserTagsRepository userTagsRepository;

    public void saveUser(UserBO userBO) {
        userBO.setStatus(0);
        userBO.setUserName(RandomUserName.createRandomName());
        userBO.setNickName(userBO.getNickname());
        userBO.setRegTime(new Date());
        userRepository.saveUser(userBO);
    }

    public void bindUsertags(UserTagsParam param) throws BusinessException {
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        if (userPrinciple==null){
            throw new BusinessException("101","用户解析异常");
        }
        Integer userId = userPrinciple.getId();
        //将param参数里的tagIds中的1,2,3转成[1,2,3]
        String tagIds = param.getTagIds();
        String[] tags= tagIds.split(",");
        List<UserTagsDO> userTagsDOS=new ArrayList<>();
        for (String tag : tags) {
            UserTagsDO userTagsDO = new UserTagsDO();
            userTagsDO.setUserId(userId);
            userTagsDO.setTagId(Integer.valueOf(tag));
            userTagsDOS.add(userTagsDO);
        }
        //删除用户绑定的标签
        userRepository.deleteUserTagsById(userId);
        userRepository.bindUsertags(userTagsDOS);
        //修改用户的状态为1
        userRepository.updateUserStatus(userId,1);
    }
    @Autowired
    private UserGroupRepository userGroupRepository;
    public BasePageList<UserGroupBO> userGroupPage(UserGroupQuery query) {
        return userGroupRepository.getUserGroupByPage(query);
    }

    public void saveUserGroup(UserGroupBO bo) throws BusinessException {
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        if (userPrinciple==null){
            throw new BusinessException("107","用户解析失败");
        }
        Integer adminId = userPrinciple.getId();
        AdminBO adminBO = adminService.adminDetail(adminId);
        if (ObjectUtils.isEmpty(adminBO)){
            throw new BusinessException("101","用户不存在");
        }
        //拿到商家id
        Integer businessId = adminBO.getBusinessId();
        bo.setBusinessId(businessId);
        bo.setCreateAt(LocalDateTime.now());
        userGroupRepository.saveUserGroup(bo);
    }

    public boolean isUserGroup(Integer userId, Integer groupId) {
        //利用userId 查询 lbs_user_tags 绑定标签列表
        List<Integer> userTagIds=userTagsRepository.getTagsByUserId(userId);
        if (CollectionUtils.isEmpty(userTagIds)){
            return false;
        }
        //userGroup
        String tagsStr=userGroupRepository.getGroupTagsById(groupId);
        if (StringUtils.isEmpty(tagsStr)){
            return false;
        }
        //做嵌套循环对比 有一个标签相等的 人群所属是true
        boolean result=false;
        String[] tagIds = tagsStr.split(",");
        for (String tagIdStr : tagIds) {
            Integer groupTagId=Integer.valueOf(tagIdStr);
            for (Integer userTagId : userTagIds) {
                if (userTagId==groupTagId){
                    result=true;
                    break;
                }
            }
        }
        return result;
    }
}
